package com.atguigu.gulimall.cart.web.controller;

import com.atguigu.gulimall.cart.service.CartService;
import com.atguigu.gulimall.cart.vo.CartItemVo;
import com.atguigu.gulimall.cart.vo.CartVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

/**
 * 购物车控制器
 *
 * @author DuGuanghan
 * @since 2025-02-14 10:34:51
 */
@Slf4j
@Controller
public class CartController {

    @Autowired
    private CartService cartService;

    /**
     * 访问购物车列表页面
     *
     * @return {@link String }
     * @author DuGuanghan
     * @since 2025-02-14 10:50:14
     */
    @RequestMapping("/cart.html")
    public String index(Model model){
        CartVo cart = cartService.getCart();

        model.addAttribute("cart", cart);
        return "cartList";
    }

    /**
     * 添加购物车
     *
     * @param skuId      SKU 编号
     * @param num        商品数量
     * @param attributes 重定向属性
     * @return {@link String }
     * @author DuGuanghan
     * @since 2025-02-17 15:44:05
     */
    @RequestMapping("/addCart")
    public String addCart(@RequestParam("skuId") Long skuId, @RequestParam("num") Integer num, RedirectAttributes attributes){
        cartService.addCart(skuId, num);

        attributes.addAttribute("skuId", skuId);
        return "redirect:http://cart.gulimall.com/successPage.html";
    }

    /**
     * 跳转至添加购物车成功页面
     *
     * @param skuId SKU 编号
     * @param model 模型
     * @return {@link String }
     * @author DuGuanghan
     * @since 2025-02-17 15:44:55
     */
    @RequestMapping("/successPage.html")
    public String successPage(@RequestParam("skuId") Long skuId, Model model){
        CartItemVo cartItemVo = cartService.getCartItem(skuId);
        model.addAttribute("cartItem", cartItemVo);

        return "success";
    }

    /**
     * 更新购物车商品选中状态
     *
     * @param skuId   SKU 编号
     * @param checked 选中状态
     * @return {@link String }
     * @author DuGuanghan
     * @since 2025-02-18 10:27:56
     */
    @RequestMapping("/checkItem")
    public String checkItem(@RequestParam("skuId") Long skuId, @RequestParam("checked") Integer checked) {
        cartService.checkItem(skuId, checked);

        return "redirect:http://cart.gulimall.com/cart.html";
    }

    /**
     * 更新购物车商品数量
     *
     * @param skuId SKU 编号
     * @param num   商品数量
     * @return {@link String }
     * @author DuGuanghan
     * @since 2025-02-18 11:50:51
     */
    @RequestMapping("/updateCartItemNum")
    public String updateCartItemNum(@RequestParam("skuId") Long skuId, @RequestParam("num") Integer num) {
        cartService.updateCartItemNum(skuId, num);

        return "redirect:http://cart.gulimall.com/cart.html";
    }

    /**
     * 删除购物车商品
     *
     * @param skuId SKU 编号
     * @return {@link String }
     * @author DuGuanghan
     * @since 2025-02-18 15:07:13
     */
    @RequestMapping("/deleteCartItem")
    public String deleteCartItem(@RequestParam("skuId") Long skuId) {
        cartService.deleteCartItem(skuId);

        return "redirect:http://cart.gulimall.com/cart.html";
    }
}
